//
//  AnchorDetailViewController.m
//  HimalayaMusic
//
//  Created by dllo on 16/1/9.
//  Copyright © 2016年 WXCLegendary. All rights reserved.
//

#import "AnchorDetailViewController.h"

@interface AnchorDetailViewController ()<UITableViewDataSource,UITableViewDelegate>

@property(nonatomic,copy) NSString *maxPageId_album;
@property(nonatomic,copy) NSString *pageSize_album;
@property(nonatomic,copy) NSString *currentPage_album;
@property(nonatomic,copy) NSString *totalCount_album;

@property(nonatomic,copy) NSString *maxPageId_track;
@property(nonatomic,copy) NSString *pageSize_track;
@property(nonatomic,copy) NSString *currentPage_track;
@property(nonatomic,copy) NSString *totalCount_track;
@property(nonatomic,assign) BOOL isload_album;
@property(nonatomic,assign) BOOL isload_track;

@property (nonatomic, strong) UITableView *tableView;

@end

@implementation AnchorDetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self getData];
    [self creatTableView];
    self.SYArray = [NSMutableArray arrayWithCapacity:0];
    self.navigationController.navigationBar.titleTextAttributes = @{NSFontAttributeName:[UIFont systemFontOfSize:15], NSForegroundColorAttributeName:kColor};
    [self getZJData:[NSString stringWithFormat:@"http://mobile.ximalaya.com/mobile/others/ca/album/%@/%@/%@?position=1&device=android&title=%@",self.uid,@"1",@"2",self.titles]];
    [self getSYData:[NSString stringWithFormat:@"http://mobile.ximalaya.com/mobile/others/ca/track/%@/%@/%@?position=1&device=android&title=%@",self.uid,@"1",@"30",self.titles]];
    
    
}

- (void)creatTableView {
    
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height ) style:UITableViewStyleGrouped];
    
    UINib *nib1 = [UINib nibWithNibName:@"ZJTableViewCell" bundle:nil];
    [self.tableView registerNib:nib1 forCellReuseIdentifier:@"ZJTableViewCell"];
    UINib *nib2 = [UINib nibWithNibName:@"SYTableViewCell" bundle:nil];
    [self.tableView registerNib:nib2 forCellReuseIdentifier:@"SYTableViewCell"];
    
    self.tableView.delegate = self;
    
    self.tableView.dataSource = self;
    
    self.isload_album = NO;
    self.isload_track = NO;
    
    
    
    [self.view addSubview:self.tableView];
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    UILabel *label = [[UILabel alloc] init];
    label.backgroundColor = [UIColor whiteColor];
    if (section == 0) {
        AbumHeaderView *album = [[AbumHeaderView alloc] initWithFrame:CGRectMake(0, 0, self.view.width, 200)];
        album.model = self.anchorModel;
        self.title = self.anchorModel.nickname;
        return album;
    } else if (section == 1) {
        
        label.text = [NSString stringWithFormat:@"发布过的专辑 (%@)", self.ZJNum];
        return label;
    } else {
        label.text = [NSString stringWithFormat:@"发布过的声音 (%@)", self.SYNum];
        return label;
        
    }
    
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    if (section == 0) {
        return 200;
    }
    return 30;
}



- (void)getData {
    [SANetWorkingTask request:self.url parmater:nil block:^(id result) {
        self.anchorModel = [[AnchorModel alloc] initWithDictionary:result];
        
        [self.tableView reloadData];
    }];
}

- (void)getZJData:(NSString *)url {
    
    
    [SANetWorkingTask request:url parmater:nil block:^(id result) {
        
        self.maxPageId_track = [result valueForKey:@"maxPageId"];
        self.pageSize_track = [result valueForKey:@"pageSize"];
        self.currentPage_track = [result valueForKey:@"pageId"];
        self.ZJNum = result[@"totalCount"];
        NSMutableArray *array = [NSMutableArray arrayWithCapacity:0];
        for (NSDictionary *dic in result[@"list"]) {
            ZJModel *model = [[ZJModel alloc] initWithDictionary:dic];
            [array addObject:model];
        }
        self.ZJArray = [NSArray arrayWithArray:array];
        [self.tableView.footer endRefreshing];
        [self.tableView reloadData];
    }];
}

- (void)getSYData:(NSString *)url {
    
    [SANetWorkingTask request:url parmater:nil block:^(id result) {
        self.maxPageId_track = [result valueForKey:@"maxPageId"];
        self.pageSize_track = [result valueForKey:@"pageSize"];
        self.currentPage_track = [result valueForKey:@"pageId"];
        self.SYNum = result[@"totalCount"];
        NSMutableArray *array = [NSMutableArray arrayWithCapacity:0];
        for (NSDictionary *dic in result[@"list"]) {
            SYModel *model = [[SYModel alloc] initWithDictionary:dic];
            [array addObject:model];
        }


//        self.SYArray = [NSMutableArray arrayWithArray:array];
        [self.SYArray addObjectsFromArray:array];
        
        [self.tableView.footer endRefreshing];
        [self.tableView reloadData];
    }];
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    
    
    if (indexPath.section == 1) {
        ZJTableViewCell *cell1 = [tableView dequeueReusableCellWithIdentifier:@"ZJTableViewCell"];
        cell1.model = self.ZJArray[indexPath.row];
        return cell1;
    } else if (indexPath.section == 2) {
        SYTableViewCell *cell2 = [tableView dequeueReusableCellWithIdentifier:@"SYTableViewCell" forIndexPath:indexPath];
        cell2.model = self.SYArray[indexPath.row];
        return cell2;
    }
    
    
    return nil;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 100;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:NO];
    if (indexPath.section == 1) {
        AlbumViewController *albumVC = [[AlbumViewController alloc] init];
        albumVC.hotRecommendListModel = [[HotRecommendListModel alloc] init];
        albumVC.hotRecommendListModel.albumId = [self.ZJArray[indexPath.row] albumId];
        [self.navigationController pushViewController:albumVC animated:YES];
    } else if (indexPath.section == 2) {
        PlayViewController *playVC = [PlayViewController sharePlayViewController];
        playVC.dataSource = self.SYArray;
        playVC.sequence = indexPath.row;
        [self presentViewController:playVC animated:YES completion:nil];
        
        
    }
    
    
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 3;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (section == 0) {
        return 0;
    } else if (section == 1) {
        return self.ZJArray.count;
    } else {
        if (self.SYArray.count) {
            return self.SYArray.count;
        } else {
            return 0;
        }
        
    }
    
}



-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    if (section == 0) {
        return CGFLOAT_MIN;
    }
    return 30;
}

-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
    UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
    button.tag = section + 10000;
    if (section == 0) {
        return nil;
    }
    
    if (section == 1) {
        if ([self.currentPage_album integerValue] < [self.maxPageId_album integerValue]) {
            self.isload_album = YES;
            [button setTitle:@"点击加载更多" forState:UIControlStateNormal];
        }else
        {
            [button setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
            [button setTitle:@"没有更多了" forState:UIControlStateNormal];
        }
    }
    if (section == 2) {
        if ([self.currentPage_track integerValue] < [self.maxPageId_track integerValue]) {
            self.isload_track = YES;
            [button setTitle:@"点击或下拉加载更多" forState:UIControlStateNormal];
        }else
        {
            [button setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
            [button setTitle:@"没有更多了" forState:UIControlStateNormal];
        }
    }
    [button addTarget:self action:@selector(loadMore:) forControlEvents:UIControlEventTouchUpInside];
    return button;
}

-(void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
    UIButton *button = [self.view.window viewWithTag:10002];
    if (scrollView.contentSize.height - 667  - 100 < self.tableView.contentOffset.y){
        [button setTitle:@"释放加载" forState:UIControlStateNormal];
    }
}

-(void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate
{
    UIButton *button = [self.view.window viewWithTag:10002];
    if (scrollView.contentSize.height - 667 - 100 < self.tableView.contentOffset.y) {
        if ([self.currentPage_track integerValue] < [self.maxPageId_track integerValue]) {
            self.isload_track = YES;
            self.currentPage_track = [NSString stringWithFormat:@"%d",[self.currentPage_track intValue] + 1];
            [self getSYData:[NSString stringWithFormat:@"http://mobile.ximalaya.com/mobile/others/ca/track/%@/%@/%@?position=1&device=android&title=%@",self.uid, self.currentPage_track,@"30",self.titles]];
            
            [button setTitle:@"正在加载..." forState:UIControlStateNormal];
            [button removeTarget:self action:@selector(loadMore:) forControlEvents:UIControlEventTouchUpInside];
        }else
        {
            [button setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
            [button setTitle:@"没有更多了" forState:UIControlStateNormal];
            [button removeTarget:self action:@selector(loadMore:) forControlEvents:UIControlEventTouchUpInside];
        }
    }
}

- (void)loadMore:(UIButton *)button {
    
    if (button.tag - 10000 == 1) {
        if (self.isload_album) {
            self.currentPage_album = [NSString stringWithFormat:@"%d", [self.currentPage_album intValue] + 1];
            [self getZJData:[NSString stringWithFormat:@"http://mobile.ximalaya.com/mobile/others/ca/album/%@/%@/%@?position=1&device=android&title=%@",self.uid,self.currentPage_album,self.pageSize_album,self.titles]];
            [button setTitle:@"正在加载..." forState:UIControlStateNormal];
            [button removeTarget:self action:@selector(loadMore:) forControlEvents:UIControlEventTouchUpInside];
        }
    }
    if (button.tag - 10000 == 2) {
        if (self.isload_track) {
            self.currentPage_track = [NSString stringWithFormat:@"%d",[self.currentPage_track intValue] + 1];
            [self getSYData:[NSString stringWithFormat:@"http://mobile.ximalaya.com/mobile/others/ca/track/%@/%@/%@?position=1&device=android&title=%@",self.uid,self.currentPage_track,@"30",self.titles]];
            [UIView animateWithDuration:1 animations:^{
                [button setTitle:@"正在加载." forState:UIControlStateNormal];
            } completion:^(BOOL finished) {
                [UIView animateWithDuration:1 animations:^{
                    [button setTitle:@"正在加载.." forState:UIControlStateNormal];
                }completion:^(BOOL finished) {
                    [UIView animateWithDuration:1 animations:^{
                        [button setTitle:@"正在加载..." forState:UIControlStateNormal];
                    }];
                }];
            }];
            
            [button removeTarget:self action:@selector(loadMore:) forControlEvents:UIControlEventTouchUpInside];
        }
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
